package com.example.qzzm;

import java.util.HashMap;
import java.util.Map;

import com.example.application.QZApplication;
import com.example.constant.App;
import com.example.jx.JsonUtils;
import com.example.net.Client;
import com.example.qzzm.R;
import com.example.vo.Model;
import com.example.vo.User;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

/**
 * 登录界面的Activity
 * 
 * @author Administrator
 *
 */
public class LoginActivity extends Activity implements TextWatcher {

	// 登录按钮
	private RelativeLayout rlLogin;
	// 返回按钮
	private ImageView ivBack;
	// 注册按钮
	private TextView tvRegister;
	// 输入框
	private EditText etAcc;
	private EditText etPwd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		initView();
		initListeners();

	}

	// 初始化布局控件
	private void initView() {
		rlLogin = (RelativeLayout) findViewById(R.id.rl_login);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		tvRegister = (TextView) findViewById(R.id.tv_register);
		etAcc = (EditText) findViewById(R.id.et_account);
		etAcc.setText("15851853306");
		etPwd = (EditText) findViewById(R.id.et_password);
		// etPwd.setText("123456");
	}

	// 初始化监听事件
	private void initListeners() {
		etPwd.addTextChangedListener(this);
		etAcc.addTextChangedListener(this);

		rlLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 跳转到主界面
				// startActivity(new Intent(LoginActivity.this,
				// GifActivity.class));

				if (TextUtils.isEmpty(etAcc.getText().toString().trim())) {
					etAcc.setError("请先输入帐号");
					return;
				}
				if (TextUtils.isEmpty(etPwd.getText().toString().trim())) {
					etPwd.setError("请输入密码");
					return;
				}
				new LoginTask().execute();
				// 关闭当前页面
				// finish();
			}
		});

		tvRegister.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(LoginActivity.this,
						RegisterActivity.class));
			}
		});

		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	class LoginTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {
			Client client = ((QZApplication) LoginActivity.this
					.getApplication()).getClient();
			Map<String, String> pairs = new HashMap<String, String>();
			pairs.put("phone", etAcc.getText().toString().trim());
			pairs.put("password", etPwd.getText().toString().trim());
			String response = client.postResquest(App.LOGIN_URL, pairs);
			User user = JsonUtils.paraseJson(response, Model.class).getData();
			if (user != null) {
				return user.getId();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			Toast.makeText(LoginActivity.this,"用户"+ result + "登录成功", Toast.LENGTH_LONG)
					.show();
			finish();
			super.onPostExecute(result);
		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		// TODO Auto-generated method stub

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (!TextUtils.isEmpty(etAcc.getText().toString().trim()) && !TextUtils.isEmpty(etPwd.getText().toString().trim())) {
			rlLogin.setBackgroundResource(R.drawable.selector_ly_loginbutton);
		} else {
			rlLogin.setBackgroundResource(R.drawable.corner_button_unnormal);
		}

	}

}
